package com.tushu.view.admin;
import com.tushu.utils.DataUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LendLog extends JPanel {
	private JTable table;

	String[] headrString = {"序号", "ID", "读者账号", "姓名", "ISBN", "书名", "借出时间", "应还时间", "状态"};

	String sql="SELECT\n" +
			"\tl.id,\n" +
			"\ts.account,\n" +
			"\ts.`name`,\n" +
			"\tb.isbn,\n" +
			"\tb.book_name,\n" +
			"\tl.lend_time,\n" +
			"\tl.return_time,\n" +
			"\tl.`status` \n" +
			"FROM\n" +
			"\ttb_lend l\n" +
			"\tLEFT JOIN tb_student s ON l.student_id = s.id\n" +
			"\tLEFT JOIN tb_book b ON b.id = l.book_id";

	String condition1=" where s.account =?";
	String condition2=" where b.isbn =?";

	String sql2="delete from tb_lend l where l.id =?";

	JTextField text;


	/**
	 * Create the panel.
	 */
	public LendLog() {
		setLayout(null);
		
		text = new JTextField();
		text.setBounds(79, 24, 219, 21);
		add(text);
		text.setColumns(10);
		
		JButton jb_search = new JButton("\u67E5\u8BE2");

		jb_search.setBounds(308, 23, 97, 23);
		add(jb_search);
		
		JButton jb_reset = new JButton("\u91CD\u7F6E");
		jb_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getAllLog(sql,null);
			}
		});
		jb_reset.setBounds(415, 23, 97, 23);
		add(jb_reset);
		
		JRadioButton rb_account = new JRadioButton("\u8BFB\u8005\u8D26\u53F7");
		rb_account.setBounds(143, 51, 84, 23);
		add(rb_account);
		
		JRadioButton rb_isbn = new JRadioButton("\u4E66\u7C4DISBN",true);
		rb_isbn.setBounds(237, 51, 127, 23);
		add(rb_isbn);

		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(rb_account);
		buttonGroup.add(rb_isbn);

		jb_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String textText = text.getText();
				if (rb_isbn.isSelected()) {
					getAllLog(sql+condition2, textText);
				}
				if (rb_account.isSelected()) {
					getAllLog(sql+condition1, textText);
				}
			}
		});

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(23, 80, 513, 289);
		add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JButton jb_delete = new JButton("\u5220\u9664");
		jb_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				int id = Integer.parseInt(table.getValueAt(row, 1).toString());
				try {
					int res = DataUtils.Update(sql2, new Object[]{id});
					if (res > 0) {
						JOptionPane.showMessageDialog(null, "成功删除一条记录！");
						getAllLog(sql,null);
					} else {
						JOptionPane.showMessageDialog(null, "操作失败！");
					}
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		jb_delete.setBounds(437, 370, 97, 23);
		add(jb_delete);
		getAllLog(sql,null);
	}


	public void getAllLog(String sql, String text) {
		if (text == null || text.equals("")) {
			try {
				ResultSet set = DataUtils.query(sql, new Object[]{});
				showAtTable(set);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		} else {
			try {
				ResultSet set = DataUtils.query(sql, new Object[]{text});
				showAtTable(set);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public void showAtTable(ResultSet set) {
		Object[][] arrays = new Object[0][];
		try {
			arrays = DataUtils.getSetArrays(set);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		DefaultTableModel model = new DefaultTableModel(arrays, headrString);
		table.setModel(model);
	}
}
